/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.analyse.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.DateUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.analyse.service.AnalyseService;
import cn.knet.sp.ncrm.modules.customer.analyse.vo.AnalyseVo;
import cn.knet.sp.ncrm.modules.customer.visitrecord.service.VisitRecordService;

/**
 * 销售跟踪Controller
 *
 * @author 冯亚鹏
 * @version 2015-08-28
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/analyse")
public class AnalyseController extends BaseController {

	@Autowired
	private VisitRecordService visitRecordService;

	@Autowired
	private AnalyseService analyseService;

	//@RequiresPermissions("customer:analyse:view")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/customer/analyse/list";//ajax模式
	}

	//@RequiresPermissions("customer:analyse:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page listData(AnalyseVo analyseVo, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(analyseVo!=null){
			if(analyseVo.getEndDate()!=null){
				analyseVo.setEndDate(DateUtils.addMilliseconds(
						analyseVo.getEndDate(),
						DateUtils.TIME_23_59_50));
			}
		}
		Page<AnalyseVo> page = visitRecordService.findPageAnalyseVo(new Page(request, response), analyseVo);
		return page;
	}

	//@RequiresPermissions("customer:analyse:view")
	@RequestMapping(value = "chart")
	public String chart() {
		return "modules/customer/analyse/chart";//ajax模式
	}

	//@RequiresPermissions("customer:analyse:view")
	@RequestMapping(value = "chartData")
	@ResponseBody
	public Object chartData(@RequestParam(value = "productTypes[]")String[] productTypes,AnalyseVo analyseVo) {
		AjaxMessage result = AjaxMessage.success();
		if(productTypes.length>0){
			analyseVo.setProducts(productTypes);
		}
		StringBuffer title = new StringBuffer();
		if(analyseVo!=null){
			if(analyseVo.getStartDate()!=null&&analyseVo.getEndDate()!=null){
				title.append(DateUtils.formatDate(analyseVo.getStartDate(),"[yyyy年MM月dd日-"));
				title.append(DateUtils.formatDate(analyseVo.getEndDate(),"yyyy年MM月dd日]"));
				analyseVo.setEndDate(DateUtils.addMilliseconds(
				analyseVo.getEndDate(),
				DateUtils.TIME_23_59_50));
			} else if(analyseVo.getEndDate()!=null){
				title.append(DateUtils.formatDate(analyseVo.getEndDate(),"[至yyyy年MM月dd日止]"));
				analyseVo.setEndDate(DateUtils.addMilliseconds(
						analyseVo.getEndDate(),
						DateUtils.TIME_23_59_50));
			} else if(analyseVo.getStartDate()!=null){
				title.append(DateUtils.formatDate(analyseVo.getStartDate(),"[yyyy年MM月dd日-当日]"));
			}
			if(title.length()==0){
				title.append("");
			}

			if(analyseVo.getMinPercentage()==0){
				title.append("[所有预计程度]");
			}else{
				title.append("[预计程度>="+analyseVo.getMinPercentage()+"%]");
			}
			title.append("销售预测图");
		}
		result.put("title",title.toString());
		try {
			List<AnalyseVo> list = visitRecordService.findChart(analyseVo);
			//long total = analyseService.totalPercentage(list);
			result.put("list",list);
			//result.put("total",total);
		} catch (Exception e){
			logger.warn("生成图表发生异常!",e);
			result.makeError("生成图表发生异常!");
		}
		return result;
	}

}